<?php


abstract class BaseTickets extends BaseObject  implements Persistent {


  const PEER = 'TicketsPeer';

	
	protected static $peer;

	
	protected $id;

	
	protected $ticketid;

	
	protected $person;

	
	protected $customer;

	
	protected $status;

	
	protected $aReceipts;

	
	protected $aPeople;

	
	protected $aCustomers;

	
	protected $collTicketliness;

	
	private $lastTicketlinesCriteria = null;

	
	protected $alreadyInSave = false;

	
	protected $alreadyInValidation = false;

	
	public function __construct()
	{
		parent::__construct();
		$this->applyDefaultValues();
	}

	
	public function applyDefaultValues()
	{
		$this->status = 0;
	}

	
	public function getId()
	{
		return $this->id;
	}

	
	public function getTicketid()
	{
		return $this->ticketid;
	}

	
	public function getPerson()
	{
		return $this->person;
	}

	
	public function getCustomer()
	{
		return $this->customer;
	}

	
	public function getStatus()
	{
		return $this->status;
	}

	
	public function setId($v)
	{
		if ($v !== null) {
			$v = (string) $v;
		}

		if ($this->id !== $v) {
			$this->id = $v;
			$this->modifiedColumns[] = TicketsPeer::ID;
		}

		if ($this->aReceipts !== null && $this->aReceipts->getId() !== $v) {
			$this->aReceipts = null;
		}

		return $this;
	} 
	
	public function setTicketid($v)
	{
		if ($v !== null) {
			$v = (int) $v;
		}

		if ($this->ticketid !== $v) {
			$this->ticketid = $v;
			$this->modifiedColumns[] = TicketsPeer::TICKETID;
		}

		return $this;
	} 
	
	public function setPerson($v)
	{
		if ($v !== null) {
			$v = (string) $v;
		}

		if ($this->person !== $v) {
			$this->person = $v;
			$this->modifiedColumns[] = TicketsPeer::PERSON;
		}

		if ($this->aPeople !== null && $this->aPeople->getId() !== $v) {
			$this->aPeople = null;
		}

		return $this;
	} 
	
	public function setCustomer($v)
	{
		if ($v !== null) {
			$v = (string) $v;
		}

		if ($this->customer !== $v) {
			$this->customer = $v;
			$this->modifiedColumns[] = TicketsPeer::CUSTOMER;
		}

		if ($this->aCustomers !== null && $this->aCustomers->getId() !== $v) {
			$this->aCustomers = null;
		}

		return $this;
	} 
	
	public function setStatus($v)
	{
		if ($v !== null) {
			$v = (int) $v;
		}

		if ($this->status !== $v || $v === 0) {
			$this->status = $v;
			$this->modifiedColumns[] = TicketsPeer::STATUS;
		}

		return $this;
	} 
	
	public function hasOnlyDefaultValues()
	{
						if (array_diff($this->modifiedColumns, array(TicketsPeer::STATUS))) {
				return false;
			}

			if ($this->status !== 0) {
				return false;
			}

				return true;
	} 
	
	public function hydrate($row, $startcol = 0, $rehydrate = false)
	{
		try {

			$this->id = ($row[$startcol + 0] !== null) ? (string) $row[$startcol + 0] : null;
			$this->ticketid = ($row[$startcol + 1] !== null) ? (int) $row[$startcol + 1] : null;
			$this->person = ($row[$startcol + 2] !== null) ? (string) $row[$startcol + 2] : null;
			$this->customer = ($row[$startcol + 3] !== null) ? (string) $row[$startcol + 3] : null;
			$this->status = ($row[$startcol + 4] !== null) ? (int) $row[$startcol + 4] : null;
			$this->resetModified();

			$this->setNew(false);

			if ($rehydrate) {
				$this->ensureConsistency();
			}

						return $startcol + 5; 
		} catch (Exception $e) {
			throw new PropelException("Error populating Tickets object", $e);
		}
	}

	
	public function ensureConsistency()
	{

		if ($this->aReceipts !== null && $this->id !== $this->aReceipts->getId()) {
			$this->aReceipts = null;
		}
		if ($this->aPeople !== null && $this->person !== $this->aPeople->getId()) {
			$this->aPeople = null;
		}
		if ($this->aCustomers !== null && $this->customer !== $this->aCustomers->getId()) {
			$this->aCustomers = null;
		}
	} 
	
	public function reload($deep = false, PropelPDO $con = null)
	{
		if ($this->isDeleted()) {
			throw new PropelException("Cannot reload a deleted object.");
		}

		if ($this->isNew()) {
			throw new PropelException("Cannot reload an unsaved object.");
		}

		if ($con === null) {
			$con = Propel::getConnection(TicketsPeer::DATABASE_NAME, Propel::CONNECTION_READ);
		}

				
		$stmt = TicketsPeer::doSelectStmt($this->buildPkeyCriteria(), $con);
		$row = $stmt->fetch(PDO::FETCH_NUM);
		$stmt->closeCursor();
		if (!$row) {
			throw new PropelException('Cannot find matching row in the database to reload object values.');
		}
		$this->hydrate($row, 0, true); 
		if ($deep) {  
			$this->aReceipts = null;
			$this->aPeople = null;
			$this->aCustomers = null;
			$this->collTicketliness = null;
			$this->lastTicketlinesCriteria = null;

		} 	}

	
	public function delete(PropelPDO $con = null)
	{
		if ($this->isDeleted()) {
			throw new PropelException("This object has already been deleted.");
		}

		if ($con === null) {
			$con = Propel::getConnection(TicketsPeer::DATABASE_NAME, Propel::CONNECTION_WRITE);
		}
		
		$con->beginTransaction();
		try {
			TicketsPeer::doDelete($this, $con);
			$this->setDeleted(true);
			$con->commit();
		} catch (PropelException $e) {
			$con->rollBack();
			throw $e;
		}
	}

	
	public function save(PropelPDO $con = null)
	{
		if ($this->isDeleted()) {
			throw new PropelException("You cannot save an object that has been deleted.");
		}

		if ($con === null) {
			$con = Propel::getConnection(TicketsPeer::DATABASE_NAME, Propel::CONNECTION_WRITE);
		}
		
		$con->beginTransaction();
		try {
			$affectedRows = $this->doSave($con);
			$con->commit();
			TicketsPeer::addInstanceToPool($this);
			return $affectedRows;
		} catch (PropelException $e) {
			$con->rollBack();
			throw $e;
		}
	}

	
	protected function doSave(PropelPDO $con)
	{
		$affectedRows = 0; 		if (!$this->alreadyInSave) {
			$this->alreadyInSave = true;

												
			if ($this->aReceipts !== null) {
				if ($this->aReceipts->isModified() || $this->aReceipts->isNew()) {
					$affectedRows += $this->aReceipts->save($con);
				}
				$this->setReceipts($this->aReceipts);
			}

			if ($this->aPeople !== null) {
				if ($this->aPeople->isModified() || $this->aPeople->isNew()) {
					$affectedRows += $this->aPeople->save($con);
				}
				$this->setPeople($this->aPeople);
			}

			if ($this->aCustomers !== null) {
				if ($this->aCustomers->isModified() || $this->aCustomers->isNew()) {
					$affectedRows += $this->aCustomers->save($con);
				}
				$this->setCustomers($this->aCustomers);
			}


						if ($this->isModified()) {
				if ($this->isNew()) {
					$pk = TicketsPeer::doInsert($this, $con);
					$affectedRows += 1; 										 										 
					$this->setNew(false);
				} else {
					$affectedRows += TicketsPeer::doUpdate($this, $con);
				}

				$this->resetModified(); 			}

			if ($this->collTicketliness !== null) {
				foreach ($this->collTicketliness as $referrerFK) {
					if (!$referrerFK->isDeleted()) {
						$affectedRows += $referrerFK->save($con);
					}
				}
			}

			$this->alreadyInSave = false;

		}
		return $affectedRows;
	} 
	
	protected $validationFailures = array();

	
	public function getValidationFailures()
	{
		return $this->validationFailures;
	}

	
	public function validate($columns = null)
	{
		$res = $this->doValidate($columns);
		if ($res === true) {
			$this->validationFailures = array();
			return true;
		} else {
			$this->validationFailures = $res;
			return false;
		}
	}

	
	protected function doValidate($columns = null)
	{
		if (!$this->alreadyInValidation) {
			$this->alreadyInValidation = true;
			$retval = null;

			$failureMap = array();


												
			if ($this->aReceipts !== null) {
				if (!$this->aReceipts->validate($columns)) {
					$failureMap = array_merge($failureMap, $this->aReceipts->getValidationFailures());
				}
			}

			if ($this->aPeople !== null) {
				if (!$this->aPeople->validate($columns)) {
					$failureMap = array_merge($failureMap, $this->aPeople->getValidationFailures());
				}
			}

			if ($this->aCustomers !== null) {
				if (!$this->aCustomers->validate($columns)) {
					$failureMap = array_merge($failureMap, $this->aCustomers->getValidationFailures());
				}
			}


			if (($retval = TicketsPeer::doValidate($this, $columns)) !== true) {
				$failureMap = array_merge($failureMap, $retval);
			}


				if ($this->collTicketliness !== null) {
					foreach ($this->collTicketliness as $referrerFK) {
						if (!$referrerFK->validate($columns)) {
							$failureMap = array_merge($failureMap, $referrerFK->getValidationFailures());
						}
					}
				}


			$this->alreadyInValidation = false;
		}

		return (!empty($failureMap) ? $failureMap : true);
	}

	
	public function getByName($name, $type = BasePeer::TYPE_PHPNAME)
	{
		$pos = TicketsPeer::translateFieldName($name, $type, BasePeer::TYPE_NUM);
		$field = $this->getByPosition($pos);
		return $field;
	}

	
	public function getByPosition($pos)
	{
		switch($pos) {
			case 0:
				return $this->getId();
				break;
			case 1:
				return $this->getTicketid();
				break;
			case 2:
				return $this->getPerson();
				break;
			case 3:
				return $this->getCustomer();
				break;
			case 4:
				return $this->getStatus();
				break;
			default:
				return null;
				break;
		} 	}

	
	public function toArray($keyType = BasePeer::TYPE_PHPNAME, $includeLazyLoadColumns = true)
	{
		$keys = TicketsPeer::getFieldNames($keyType);
		$result = array(
			$keys[0] => $this->getId(),
			$keys[1] => $this->getTicketid(),
			$keys[2] => $this->getPerson(),
			$keys[3] => $this->getCustomer(),
			$keys[4] => $this->getStatus(),
		);
		return $result;
	}

	
	public function setByName($name, $value, $type = BasePeer::TYPE_PHPNAME)
	{
		$pos = TicketsPeer::translateFieldName($name, $type, BasePeer::TYPE_NUM);
		return $this->setByPosition($pos, $value);
	}

	
	public function setByPosition($pos, $value)
	{
		switch($pos) {
			case 0:
				$this->setId($value);
				break;
			case 1:
				$this->setTicketid($value);
				break;
			case 2:
				$this->setPerson($value);
				break;
			case 3:
				$this->setCustomer($value);
				break;
			case 4:
				$this->setStatus($value);
				break;
		} 	}

	
	public function fromArray($arr, $keyType = BasePeer::TYPE_PHPNAME)
	{
		$keys = TicketsPeer::getFieldNames($keyType);

		if (array_key_exists($keys[0], $arr)) $this->setId($arr[$keys[0]]);
		if (array_key_exists($keys[1], $arr)) $this->setTicketid($arr[$keys[1]]);
		if (array_key_exists($keys[2], $arr)) $this->setPerson($arr[$keys[2]]);
		if (array_key_exists($keys[3], $arr)) $this->setCustomer($arr[$keys[3]]);
		if (array_key_exists($keys[4], $arr)) $this->setStatus($arr[$keys[4]]);
	}

	
	public function buildCriteria()
	{
		$criteria = new Criteria(TicketsPeer::DATABASE_NAME);

		if ($this->isColumnModified(TicketsPeer::ID)) $criteria->add(TicketsPeer::ID, $this->id);
		if ($this->isColumnModified(TicketsPeer::TICKETID)) $criteria->add(TicketsPeer::TICKETID, $this->ticketid);
		if ($this->isColumnModified(TicketsPeer::PERSON)) $criteria->add(TicketsPeer::PERSON, $this->person);
		if ($this->isColumnModified(TicketsPeer::CUSTOMER)) $criteria->add(TicketsPeer::CUSTOMER, $this->customer);
		if ($this->isColumnModified(TicketsPeer::STATUS)) $criteria->add(TicketsPeer::STATUS, $this->status);

		return $criteria;
	}

	
	public function buildPkeyCriteria()
	{
		$criteria = new Criteria(TicketsPeer::DATABASE_NAME);

		$criteria->add(TicketsPeer::ID, $this->id);

		return $criteria;
	}

	
	public function getPrimaryKey()
	{
		return $this->getId();
	}

	
	public function setPrimaryKey($key)
	{
		$this->setId($key);
	}

	
	public function copyInto($copyObj, $deepCopy = false)
	{

		$copyObj->setId($this->id);

		$copyObj->setTicketid($this->ticketid);

		$copyObj->setPerson($this->person);

		$copyObj->setCustomer($this->customer);

		$copyObj->setStatus($this->status);


		if ($deepCopy) {
									$copyObj->setNew(false);

			foreach ($this->getTicketliness() as $relObj) {
				if ($relObj !== $this) {  					$copyObj->addTicketlines($relObj->copy($deepCopy));
				}
			}

		} 

		$copyObj->setNew(true);

	}

	
	public function copy($deepCopy = false)
	{
				$clazz = get_class($this);
		$copyObj = new $clazz();
		$this->copyInto($copyObj, $deepCopy);
		return $copyObj;
	}

	
	public function getPeer()
	{
		if (self::$peer === null) {
			self::$peer = new TicketsPeer();
		}
		return self::$peer;
	}

	
	public function setReceipts(Receipts $v = null)
	{
		if ($v === null) {
			$this->setId(NULL);
		} else {
			$this->setId($v->getId());
		}

		$this->aReceipts = $v;

				if ($v !== null) {
			$v->setTickets($this);
		}

		return $this;
	}


	
	public function getReceipts(PropelPDO $con = null)
	{
		if ($this->aReceipts === null && (($this->id !== "" && $this->id !== null))) {
			$c = new Criteria(ReceiptsPeer::DATABASE_NAME);
			$c->add(ReceiptsPeer::ID, $this->id);
			$this->aReceipts = ReceiptsPeer::doSelectOne($c, $con);
						$this->aReceipts->setTickets($this);
		}
		return $this->aReceipts;
	}

	
	public function setPeople(People $v = null)
	{
		if ($v === null) {
			$this->setPerson(NULL);
		} else {
			$this->setPerson($v->getId());
		}

		$this->aPeople = $v;

						if ($v !== null) {
			$v->addTickets($this);
		}

		return $this;
	}


	
	public function getPeople(PropelPDO $con = null)
	{
		if ($this->aPeople === null && (($this->person !== "" && $this->person !== null))) {
			$c = new Criteria(PeoplePeer::DATABASE_NAME);
			$c->add(PeoplePeer::ID, $this->person);
			$this->aPeople = PeoplePeer::doSelectOne($c, $con);
			
		}
		return $this->aPeople;
	}

	
	public function setCustomers(Customers $v = null)
	{
		if ($v === null) {
			$this->setCustomer(NULL);
		} else {
			$this->setCustomer($v->getId());
		}

		$this->aCustomers = $v;

						if ($v !== null) {
			$v->addTickets($this);
		}

		return $this;
	}


	
	public function getCustomers(PropelPDO $con = null)
	{
		if ($this->aCustomers === null && (($this->customer !== "" && $this->customer !== null))) {
			$c = new Criteria(CustomersPeer::DATABASE_NAME);
			$c->add(CustomersPeer::ID, $this->customer);
			$this->aCustomers = CustomersPeer::doSelectOne($c, $con);
			
		}
		return $this->aCustomers;
	}

	
	public function clearTicketliness()
	{
		$this->collTicketliness = null; 	}

	
	public function initTicketliness()
	{
		$this->collTicketliness = array();
	}

	
	public function getTicketliness($criteria = null, PropelPDO $con = null)
	{
		if ($criteria === null) {
			$criteria = new Criteria(TicketsPeer::DATABASE_NAME);
		}
		elseif ($criteria instanceof Criteria)
		{
			$criteria = clone $criteria;
		}

		if ($this->collTicketliness === null) {
			if ($this->isNew()) {
			   $this->collTicketliness = array();
			} else {

				$criteria->add(TicketlinesPeer::TICKET, $this->id);

				TicketlinesPeer::addSelectColumns($criteria);
				$this->collTicketliness = TicketlinesPeer::doSelect($criteria, $con);
			}
		} else {
						if (!$this->isNew()) {
												

				$criteria->add(TicketlinesPeer::TICKET, $this->id);

				TicketlinesPeer::addSelectColumns($criteria);
				if (!isset($this->lastTicketlinesCriteria) || !$this->lastTicketlinesCriteria->equals($criteria)) {
					$this->collTicketliness = TicketlinesPeer::doSelect($criteria, $con);
				}
			}
		}
		$this->lastTicketlinesCriteria = $criteria;
		return $this->collTicketliness;
	}

	
	public function countTicketliness(Criteria $criteria = null, $distinct = false, PropelPDO $con = null)
	{
		if ($criteria === null) {
			$criteria = new Criteria(TicketsPeer::DATABASE_NAME);
		} else {
			$criteria = clone $criteria;
		}

		if ($distinct) {
			$criteria->setDistinct();
		}

		$count = null;

		if ($this->collTicketliness === null) {
			if ($this->isNew()) {
				$count = 0;
			} else {

				$criteria->add(TicketlinesPeer::TICKET, $this->id);

				$count = TicketlinesPeer::doCount($criteria, $con);
			}
		} else {
						if (!$this->isNew()) {
												

				$criteria->add(TicketlinesPeer::TICKET, $this->id);

				if (!isset($this->lastTicketlinesCriteria) || !$this->lastTicketlinesCriteria->equals($criteria)) {
					$count = TicketlinesPeer::doCount($criteria, $con);
				} else {
					$count = count($this->collTicketliness);
				}
			} else {
				$count = count($this->collTicketliness);
			}
		}
		$this->lastTicketlinesCriteria = $criteria;
		return $count;
	}

	
	public function addTicketlines(Ticketlines $l)
	{
		if ($this->collTicketliness === null) {
			$this->initTicketliness();
		}
		if (!in_array($l, $this->collTicketliness, true)) { 			array_push($this->collTicketliness, $l);
			$l->setTickets($this);
		}
	}


	
	public function getTicketlinessJoinProducts($criteria = null, $con = null, $join_behavior = Criteria::LEFT_JOIN)
	{
		if ($criteria === null) {
			$criteria = new Criteria(TicketsPeer::DATABASE_NAME);
		}
		elseif ($criteria instanceof Criteria)
		{
			$criteria = clone $criteria;
		}

		if ($this->collTicketliness === null) {
			if ($this->isNew()) {
				$this->collTicketliness = array();
			} else {

				$criteria->add(TicketlinesPeer::TICKET, $this->id);

				$this->collTicketliness = TicketlinesPeer::doSelectJoinProducts($criteria, $con, $join_behavior);
			}
		} else {
									
			$criteria->add(TicketlinesPeer::TICKET, $this->id);

			if (!isset($this->lastTicketlinesCriteria) || !$this->lastTicketlinesCriteria->equals($criteria)) {
				$this->collTicketliness = TicketlinesPeer::doSelectJoinProducts($criteria, $con, $join_behavior);
			}
		}
		$this->lastTicketlinesCriteria = $criteria;

		return $this->collTicketliness;
	}


	
	public function getTicketlinessJoinTaxes($criteria = null, $con = null, $join_behavior = Criteria::LEFT_JOIN)
	{
		if ($criteria === null) {
			$criteria = new Criteria(TicketsPeer::DATABASE_NAME);
		}
		elseif ($criteria instanceof Criteria)
		{
			$criteria = clone $criteria;
		}

		if ($this->collTicketliness === null) {
			if ($this->isNew()) {
				$this->collTicketliness = array();
			} else {

				$criteria->add(TicketlinesPeer::TICKET, $this->id);

				$this->collTicketliness = TicketlinesPeer::doSelectJoinTaxes($criteria, $con, $join_behavior);
			}
		} else {
									
			$criteria->add(TicketlinesPeer::TICKET, $this->id);

			if (!isset($this->lastTicketlinesCriteria) || !$this->lastTicketlinesCriteria->equals($criteria)) {
				$this->collTicketliness = TicketlinesPeer::doSelectJoinTaxes($criteria, $con, $join_behavior);
			}
		}
		$this->lastTicketlinesCriteria = $criteria;

		return $this->collTicketliness;
	}

	
	public function clearAllReferences($deep = false)
	{
		if ($deep) {
			if ($this->collTicketliness) {
				foreach ((array) $this->collTicketliness as $o) {
					$o->clearAllReferences($deep);
				}
			}
		} 
		$this->collTicketliness = null;
			$this->aReceipts = null;
			$this->aPeople = null;
			$this->aCustomers = null;
	}

} 